package robren.data;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import robren.domain.Solicitante;



public class JDBCSolicitanteDAO implements ISolicitanteDAO {

	private Connection conn;

	public JDBCSolicitanteDAO(){
		conn = ConnectionManager.getInstance().checkOut();
	}
	
	 protected void finalize() {
	        ConnectionManager.getInstance().checkIn(conn);
	    }
	public void updatePuntos(String id, Integer puntos) {
		String sql = "UPDATE solicitante SET Puntos = ? WHERE(id_solicitante = ?)";
		PreparedStatement stmt = null;

		try {
			stmt = conn.prepareStatement(sql);
			stmt.setInt(1, puntos);
			stmt.setString(2, id);
			stmt.executeUpdate();
			}
		catch (SQLException e) {
			
			System.out.println("Message: " + e.getMessage());
			System.out.println("SQLState: " + e.getSQLState());
			System.out.println("ErrorCode: " + e.getErrorCode());
		} finally {
			try {
				if (stmt != null) {
					stmt.close();
				}
			} catch (SQLException e) {
			}
		}

	

	}

	
	public void insertSolicitante(Solicitante sol,String OID) {
		String sql = "INSERT INTO solicitante ( Nombre, id_solicitante, direccion, nick, password, telefono, email, Puntos,sector,OID) VALUES (?,?, ?, ?, ?, ?, ?, ?, ?,?) ";
        PreparedStatement stmt = null;

        try {
            stmt = conn.prepareStatement(sql);

            stmt.setString(1, sol.getNombre());
            stmt.setString(2, sol.getId_Solicitante());
            stmt.setString(3, sol.getDireccion());
            stmt.setString(4, sol.getNick());
            stmt.setString(5, sol.getPassword());
            stmt.setInt(6, sol.getTelefono());
            stmt.setString(7, sol.getEmail());
            stmt.setInt(8, sol.getPuntos());
            stmt.setString(9, sol.getSector());
            stmt.setString(10, OID);

            stmt.executeUpdate();

        } catch (SQLException e) {
            System.out.println("Message: " + e.getMessage());
            System.out.println("SQLState: " + e.getSQLState());
            System.out.println("ErrorCode: " + e.getErrorCode());
        } finally {
            try {
                if (stmt != null) {
                    stmt.close();
                }
            } catch (SQLException e) {
            }
        }
		
	}

	
	public boolean Logado(String nic, String pass) {

		boolean respuesta = false;
		
		String sql = "SELECT * FROM solicitante WHERE (nick = ? AND password = ?) ";
        PreparedStatement stmt = null;
        ResultSet result = null;
 
        try {

            stmt = conn.prepareStatement(sql); 
            stmt.setString(1, nic);
            stmt.setString(2, pass);
            result = stmt.executeQuery();
           
            if(result.next())
            	respuesta =true;
  
            
        } catch (SQLException e) {
            System.out.println("Message: " + e.getMessage());
            System.out.println("SQLState: " + e.getSQLState());
            System.out.println("ErrorCode: " + e.getErrorCode());
        } finally {
            try {
                if (result != null)
                    result.close();
                if (stmt != null)
                    stmt.close();
            } catch (SQLException e) {
            }

        }
		
        return respuesta;
	}

	@Override
	public String selectId(String nick) {

		String id_solicitante=null;
		String sql = "SELECT id_solicitante FROM solicitante WHERE (nick = ?) ";
        PreparedStatement stmt = null;
        ResultSet result = null;
        
        try {

            stmt = conn.prepareStatement(sql); 
            stmt.setString(1, nick);
            result = stmt.executeQuery();
            while (result.next())
            	id_solicitante =(result.getString("id_solicitante"));  
            
        } catch (SQLException e) {
            System.out.println("Message: " + e.getMessage());
            System.out.println("SQLState: " + e.getSQLState());
            System.out.println("ErrorCode: " + e.getErrorCode());
        } finally {
            try {
                if (result != null)
                    result.close();
                if (stmt != null)
                    stmt.close();
            } catch (SQLException e) {
            }

        }
        return id_solicitante;
	}
	public String selectOidSolicitante(String id){
		PreparedStatement stmt = null;
        ResultSet result = null;
        String oidp = null;
        String sql = "SELECT * FROM solicitante WHERE (id_solicitante=?) ";

        try {
            stmt = conn.prepareStatement(sql);
            stmt.setString(1, id);
            result = stmt.executeQuery();

            result.next();
            oidp = result.getString("OID");
            
        } catch (SQLException e) {
            System.out.println("Message: " + e.getMessage());
            System.out.println("SQLState: " + e.getSQLState());
            System.out.println("ErrorCode: " + e.getErrorCode());
        } finally {
            try {
                if (result != null) {
                    result.close();
                }
                if (stmt != null) {
                    stmt.close();
                }
            } catch (SQLException e) {
            }
        }
        return oidp;
	}

}
